<template>
  <div class="app-container" v-loading="pageLoading">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户货物" prop="customerGoods">
        <el-input
          v-model="queryParams.customerGoods"
          placeholder="请输入客户货物"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户姓名" align="center" prop="customerName" min-width="120" fixed="left"/>
      <el-table-column label="客户手机" align="center" prop="customerCode" min-width="130"/>
      <el-table-column label="客户货物" align="center" prop="customerPhone" min-width="130"/>
      <el-table-column label="货物名称" align="center" prop="goodsName" min-width="160"/>
      <el-table-column label="货物数量" align="center" prop="total" min-width="100"/>
      <el-table-column label="包装数量" align="center" prop="packageNum" min-width="100"/>
      <el-table-column label="货物体积" align="center" prop="goodsSize" min-width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="160"/>
      <el-table-column label="操作" align="center" min-width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改货柜货物对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               :close-on-click-modal="false"
               width="500px"
               append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="userLoading">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" @blur="changeUser"/>
        </el-form-item>
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" disabled/>
        </el-form-item>
        <el-form-item label="客户手机" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户手机" :disabled="form.customerId != null"/>
        </el-form-item>
        <el-form-item label="客户货物" prop="customerGoods">
          <el-input v-model="form.customerGoods" placeholder="请输入客户货物" />
        </el-form-item>
        <el-form-item label="货物数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入货物数量" />
        </el-form-item>
        <el-form-item label="包装数据" prop="cargoPackage">
          <el-input v-model="form.cargoPackage" placeholder="请输入包装数量" />
        </el-form-item>
        <el-form-item label="货物体积" prop="goodsSquare">
          <el-input v-model="form.goodsSquare" placeholder="请输入货物体积" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/system/counter-goods"
import { getCustomerInfo } from '@/api/system/common'

export default {
  name: "goods-list",
  data() {
    return {
      pageLoading: false,
      // 遮罩层
      loading: true,
      userLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 货柜货物表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        counterId: this.$route.params.id,
        customerName: null,
        customerGoods: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" }
        ],
        customerGoods: [
          { required: true, message: "客户货物不能为空", trigger: "blur" }
        ],
        goodsNum: [
          { required: true, message: "货物数量不能为空", trigger: "blur" }
        ],
        cargoPackage: [
          { required: true, message: "包装数量不能为空", trigger: "blur" }
        ],
        goodsSquare: [
          { required: true, message: "货物体积不能为空", trigger: "blur" }
        ],
      }
    }
  },
  async created() {
    this.getList();
  },
  methods: {
    /** 查询货柜货物列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.data;
        this.loading = false;
      }).catch((e) => {
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        counterId: null,
        customerId: null,
        customerName: null,
        customerCode: null,
        customerPhone: null,
        customerGoods: null,
        goodsNum: null,
        goodsSquare: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.counterId = this.$route.params.id;
      this.title = "添加货柜货物";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货柜货物";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除货柜货物编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/system/counter/goods/export', {
        ...this.queryParams
      }, `货柜货物_${new Date().getTime()}.xlsx`)
    },
    /** 切换用户 */
    async changeUser(){
      if (this.form.customerName.trim() != '') {
        try {
          this.userLoading = true;
          const { data } = await getCustomerInfo(this.form.customerName.trim());
          this.form.customerId = data.userId;
          this.form.customerName = data.username;
          this.form.customerCode = data.code;
          this.form.customerPhone = data.phone;
          this.userLoading = false;
        } catch (e) {
          this.userLoading = false;
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.app-container {
}
</style>
